<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/3
 * Time: 18:51
 */

namespace Common\Util;


use Think\Log;

class PushUtil
{

    public static function pushMsgToApp($cid,$msg) {
        $igt = new \IGeTui(C('PUSH_HOST'),C('PUSH_APPKEY'),C('PUSH_MASTERSECRET'));
        //定义透传模板，设置透传内容，和收到消息是否立即启动启用
        $template = self::getTemplate($msg);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        $message = new \IGtSingleMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(3600*1000*72);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);

        $target = new \IGtTarget();
        $target->set_appId(C('PUSH_APPID'));
        $target->set_clientId($cid);

        $rep ='';
        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            Log::write($rep);
        }catch(\RequestException $e){
            Log::write($e);
        }


        return $rep;
    }

    private  function getTemplate($msg){
        $template =  new \IGtNotificationTemplate();
        $template->set_appId(C('PUSH_APPID'));                   //应用appid
        $template->set_appkey(C('PUSH_APPKEY'));                 //应用appkey
        $template->set_transmissionType(1);            //透传消息类型
        $template->set_transmissionContent("丰乐玉米消息推送");//透传内容
        $template->set_title("丰乐玉米消息推送");                  //通知栏标题
        $template->set_text($msg);     //通知栏内容
        $template->set_logo("");                       //通知栏logo
        $template->set_logoURL("");                    //通知栏logo链接
        $template->set_isRing(true);                   //是否响铃
        $template->set_isVibrate(true);                //是否震动
        $template->set_isClearable(true);              //通知栏是否可清除

        return $template;
    }


}